class Admin::ProductAttributesController < Admin::BaseController

  before_filter :find_product

  # GET /product_attributes/new
  # GET /product_attributes/new.xml
  def new
    @product_attribute = @product.product_attributes.new
    @attributes = Attribute.all
  end

  # GET /product_attributes/1/edit
  def edit
    @product_attribute = @product.product_attributes.find(params[:id])

    respond_to do |respond|
      respond.html
      respond.js
    end
  end

  # POST /product_attributes
  # POST /product_attributes.xml
  def create
    @product_attribute = @product.product_attributes.new(params[:product_attribute])
    @success = @product_attribute.save

    respond_to do |respond|
      respond.html do
        if !@success
          render :action => 'new'
        else
          redirect_to edit_admin_product_path( @product )
        end
      end

      respond.js
    end
  end

  # PUT /product_attributes/1
  # PUT /product_attributes/1.xml
  def update
    @product_attribute = @product.product_attributes.find(params[:id])
    @success = @product_attribute.update_attributes(params[:product_attribute])

    respond_to do |respond|
      respond.html do
        if @success
          flash[:notice] = 'Attribuut is aangepast.'
          redirect_to edit_admin_product_path( @product )
        else
          render :action => "edit"
        end
      end

      respond.js
    end
  end

  # DELETE /product_attributes/1
  # DELETE /product_attributes/1.xml
  def destroy
    @product_attribute = @product.product_attributes.find(params[:id])
    @success = @product_attribute.destroy

    respond_to do |respond|
      respond.html do

        if @success
          flash[:notice] = 'Attribuut is verwijderd.'
        else
          flash[:notice] = 'Attribuut is niet verwijderd.'
        end

        redirect_to edit_admin_product_path( @product )
      end

      respond.js
    end
    
  end

  private
    def find_product
      @product = Product.find(params[:product_id])
    end
end
